<html>
<head><meta charset="utf-8"><title>safe self references · wg-async-foundations · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/index.html">wg-async-foundations</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html">safe self references</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="245494025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494025">(Jul 09 2021 at 20:31)</a>:</h4>
<p>A related question: supposing that we get support from Polonius in the future, will we be able to support safe self-references without actually needing Pin?</p>



<a name="245494076"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494076" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494076">(Jul 09 2021 at 20:32)</a>:</h4>
<p>I think the main times that people interact with poll today are less often writing futures as they are writing <em>other things</em> (async-read, async-write, streams)</p>



<a name="245494096"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494096" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494096">(Jul 09 2021 at 20:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/187312-wg-async-foundations/topic/Dropping.20.60.2Eawait.60-.20what.20else.20could.20or.20would.20change/near/245494025">said</a>:</p>
<blockquote>
<p>A related question: supposing that we get support from Polonius in the future, will we be able to support safe self-references without actually needing Pin?</p>
</blockquote>
<p>I believe so, but not necessarily every pattern</p>



<a name="245494133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494133">(Jul 09 2021 at 20:32)</a>:</h4>
<p>World's simplest self-referential data structure:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">SelfRef</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">counter_1</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">counter_2</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">current_counter</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="245494154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494154">(Jul 09 2021 at 20:32)</a>:</h4>
<p>depends on your POV</p>



<a name="245494179"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494179" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494179">(Jul 09 2021 at 20:33)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">SelfRef</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">data</span>: <span class="nc">Rc</span><span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">u32</span><span class="o">&gt;&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">pointer_into_data</span>: <span class="kp">&amp;</span><span class="kt">u32</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>is simpler in some ways :)</p>



<a name="245494227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494227">(Jul 09 2021 at 20:33)</a>:</h4>
<p>iow, I think there might be a world where you can have references, but the data you are referring to has to be "in the heap"</p>



<a name="245494232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494232">(Jul 09 2021 at 20:34)</a>:</h4>
<p>Perhaps, but I wanted mutation and I wanted to dodge the question of how to prevent mutating the Vec while that reference exists.</p>



<a name="245494250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494250">(Jul 09 2021 at 20:34)</a>:</h4>
<p>such that the struct can stll be moved</p>



<a name="245494291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494291">(Jul 09 2021 at 20:34)</a>:</h4>
<p>well, the point is, if the borrow checker knows you have references into <code>self.data</code></p>



<a name="245494307"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494307" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494307">(Jul 09 2021 at 20:34)</a>:</h4>
<p>it could potentially treat that field as borrowed</p>



<a name="245494328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494328">(Jul 09 2021 at 20:34)</a>:</h4>
<p>really I should've just put <code>Vec&lt;u32&gt;</code>, the <code>Rc</code> is a distraction</p>



<a name="245494337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494337">(Jul 09 2021 at 20:34)</a>:</h4>
<p>anyway, my point is only that we can separate out: references <em>to</em> other fields</p>



<a name="245494341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494341">(Jul 09 2021 at 20:34)</a>:</h4>
<p>from references to <em>data owned by other fields</em></p>



<a name="245494353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494353">(Jul 09 2021 at 20:34)</a>:</h4>
<p>I could imagine one being easier than the other</p>



<a name="245494387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494387">(Jul 09 2021 at 20:35)</a>:</h4>
<p>and it's probably worth distinguishing, because a key use case for my example is to be able to move that between threads etc</p>



<a name="245494396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494396">(Jul 09 2021 at 20:35)</a>:</h4>
<p>That's fair, and references to heap memory only would still be helpful in some ways, but there are valid use cases for self-references within the same struct.</p>



<a name="245494409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494409">(Jul 09 2021 at 20:35)</a>:</h4>
<p>yes, for sure</p>



<a name="245494410"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494410" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494410">(Jul 09 2021 at 20:35)</a>:</h4>
<p>And, for that matter, references that <em>might</em> be self-references.</p>



<a name="245494491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494491">(Jul 09 2021 at 20:36)</a>:</h4>
<p>I'd like to be able to do things like allocating a struct with extra memory hanging off the end and making an internal reference within the struct point into that extra memory, so that I have one allocation.</p>



<a name="245494534"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494534" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494534">(Jul 09 2021 at 20:36)</a>:</h4>
<p>I'm just trying to say why I'm not sure whether we would get both or not, I've thought more about the "data owned by other fields" case than I have about "pointers to felds"</p>



<a name="245494564"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494564" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494564">(Jul 09 2021 at 20:36)</a>:</h4>
<p>they are actually very different, I realize now</p>



<a name="245494579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494579">(Jul 09 2021 at 20:36)</a>:</h4>
<p>in that the former is something "internal" to the struct, and the latter is "external"</p>



<a name="245494581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494581">(Jul 09 2021 at 20:36)</a>:</h4>
<p><code>struct Thing { ref_that_might_be_a_selfref: &amp;mut [u8], attached_data: [u8] }</code></p>



<a name="245494624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494624">(Jul 09 2021 at 20:37)</a>:</h4>
<p>Move that into another thread <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="245494675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/safe%20self%20references/near/245494675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/safe.20self.20references.html#245494675">(Jul 09 2021 at 20:37)</a>:</h4>
<p>yes :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>